package com.cuit.jifver.weibo.activity;

import com.cuit.jifver.weibo.R;
import com.cuit.jifver.weibo.data.Weibo;
import com.cuit.jifver.weibo.db.AppRecordData;
import com.cuit.jifver.weibo.utils.ConfigUtil;
import com.cuit.jifver.weibo.utils.Constants;
import com.cuit.jifver.weibo.utils.NetUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;



/**
 * 登录选择界面
 *		1.根据不同的按钮点击初始化数据 		
 * @author bywyu
 */
public class MainAct extends BaseActivity implements OnClickListener{
	private static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button sinaBtn = (Button) findViewById(R.id.sinaBtn);
        sinaBtn.setOnClickListener(this);
        sinaBtn.setTag(ConfigUtil.SINAW);
        
        Button qqBtn = (Button) findViewById(R.id.qqBtn);
        qqBtn.setOnClickListener(this);
        qqBtn.setTag(ConfigUtil.QQW);
        
        Button sohuBtn = (Button) findViewById(R.id.sohuBtn);
        sohuBtn.setOnClickListener(this);
        sohuBtn.setTag(ConfigUtil.SOHUW);
        
        Button wangyiBtn = (Button) findViewById(R.id.wangyiBtn);
        wangyiBtn.setOnClickListener(this);
        wangyiBtn.setTag(ConfigUtil.WANGYIW);
    }

	@Override
    public void onClick(View v) {
		
		//初始化ConfigUtil信息
		ConfigUtil conf = ConfigUtil.getInstance();
		String curWeibo = String.valueOf(v.getTag());
		conf.setCurWeibo(curWeibo);
		
		if(curWeibo.equals(ConfigUtil.SINAW)){
			conf.initSinaData();
		}else if(curWeibo.equals(ConfigUtil.QQW)){
			conf.initQqData();
		}else if(curWeibo.equals(ConfigUtil.SOHUW)){
			conf.initSohuData();
		}else if(curWeibo.equals(ConfigUtil.WANGYIW)){
			conf.initWangyiData();
		}
		
		boolean networkIsUsed = NetUtil.isNetworkAvailable(this);
		if (!networkIsUsed)
		{
			Toast.makeText(this, "Network is unavailable,please try again later!!!", Toast.LENGTH_SHORT);
			return;
		}
		
		if (!AppRecordData.isUserAuthorized() )
		{
			Intent intent = new Intent(MainAct.this, AuthorizationAct.class);
			startActivity(intent);
		}
		else 
		{
			Intent intent = new Intent(MainAct.this, HomePageAct.class);
			startActivity(intent);
		}
		
    }
	
	

	private boolean userAuthorized()
	{
	    SharedPreferences sharedPreferences = getSharedPreferences(Constants.oauth.OAUTH_FILE_NAME, MODE);
	    String accessToken = sharedPreferences.getString(ConfigUtil.access_token_name, null);
	    String accessTokenSecret = sharedPreferences.getString(ConfigUtil.access_token_secret_name, null);
	    if (accessToken != null && accessTokenSecret != null)
		{
			
			Weibo weibo = Weibo.getInstance();
			weibo.setAccessToken(accessToken);
			weibo.setAccessSecret(accessTokenSecret);
			return true;
		}
		
		
		return false;
	}

	@Override
	public void refresh(Object... param)
	{
		// TODO Auto-generated method stub
		
	}
}